package com.flyduck.mall.pms.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyduck.mall.pms.domain.PmsProduct;
import com.flyduck.mall.pms.domain.PmsSkuStock;
import com.flyduck.mall.pms.domain.PmsSpecification;
import com.flyduck.mall.pms.domain.PmsSpecificationValue;
import com.flyduck.mall.pms.mapper.PmsProductMapper;
import com.flyduck.mall.pms.mapper.PmsSkuStockMapper;
import com.flyduck.mall.pms.mapper.PmsSpecificationMapper;
import com.flyduck.mall.pms.mapper.PmsSpecificationValueMapper;
import com.flyduck.ss.common.core.exception.ServiceException;

import com.flyduck.mall.common.core.utils.BeanUtils;
import com.flyduck.mall.pms.admin.service.AdminSkuService;
import com.flyduck.mall.pms.admin.vo.AdminSkuDetailsRespVO;
import com.flyduck.mall.pms.admin.vo.AdminSkuSaveReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * AdminSkuServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2024-06-06
 */
@Slf4j
@Service
public class AdminSkuServiceImpl implements AdminSkuService {

    @Resource
    private PmsProductMapper pmsProductMapper;
    @Resource
    private PmsSpecificationMapper pmsSpecificationMapper;
    @Resource
    private PmsSpecificationValueMapper pmsSpecificationValueMapper;
    @Resource
    private PmsSkuStockMapper pmsSkuStockMapper;

    @Override
    public AdminSkuDetailsRespVO getSkuDetailsByProductId(Long productId) {
        AdminSkuDetailsRespVO adminSkuDetailsRespVO = new AdminSkuDetailsRespVO();
        //商品信息
        PmsProduct pmsProduct = pmsProductMapper.selectById(productId);
        if (pmsProduct == null) {
            throw new ServiceException("商品不存在");
        }
        AdminSkuDetailsRespVO.PmsProductVO product = BeanUtils.toBean(pmsProduct, AdminSkuDetailsRespVO.PmsProductVO.class);
        adminSkuDetailsRespVO.setProduct(product);

        //获取所有内置规格
        List<PmsSpecification> pmsSpecificationList = pmsSpecificationMapper.selectList(
                new LambdaQueryWrapper<PmsSpecification>()
        );
        List<AdminSkuDetailsRespVO.PmsSpecificationVO> specs = BeanUtils.toBeanList(pmsSpecificationList, AdminSkuDetailsRespVO.PmsSpecificationVO.class);
        adminSkuDetailsRespVO.setSpecs(specs);

        //获取该商品对应的规格值
        List<PmsSpecificationValue> pmsSpecificationValueList = pmsSpecificationValueMapper.selectList(
                new LambdaQueryWrapper<PmsSpecificationValue>()
                        .eq(PmsSpecificationValue::getProductId, productId)
        );
        List<AdminSkuDetailsRespVO.PmsSpecificationValueVO> specValues = BeanUtils.toBeanList(pmsSpecificationValueList, AdminSkuDetailsRespVO.PmsSpecificationValueVO.class);
        adminSkuDetailsRespVO.setSpecValues(specValues);

        //获取选中的规格值的名称组合
        List<String> selectSpecValues = pmsSpecificationValueList.stream()
                .map(PmsSpecificationValue::getSpec)
                .collect(Collectors.toList());
        adminSkuDetailsRespVO.setSelectSpecValues(selectSpecValues);

        //获取该商品sku的信息
        List<PmsSkuStock> pmsSkuStockList = pmsSkuStockMapper.selectList(
                new LambdaQueryWrapper<PmsSkuStock>()
                        .eq(PmsSkuStock::getProductId, productId)
        );
        List<AdminSkuDetailsRespVO.PmsSkuStockVO> skuStocks = BeanUtils.toBeanList(pmsSkuStockList, AdminSkuDetailsRespVO.PmsSkuStockVO.class);
        adminSkuDetailsRespVO.setSkuStocks(skuStocks);

        return adminSkuDetailsRespVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSku(AdminSkuSaveReqVO adminSkuSaveReqVO) {
        //先删除原来的规格值，再保存商品规格值
        pmsSpecificationValueMapper.delete(
                new LambdaQueryWrapper<PmsSpecificationValue>()
                .eq(PmsSpecificationValue::getProductId,adminSkuSaveReqVO.getProductId())
        );

        List<AdminSkuSaveReqVO.PmsSpecificationValueVO> specValues = adminSkuSaveReqVO.getSpecValues();
        if(CollectionUtil.isNotEmpty(specValues)){
            List<PmsSpecificationValue> pmsSpecificationValues = BeanUtils.toBeanList(specValues, PmsSpecificationValue.class);
            pmsSpecificationValues.forEach(appSpecificationValue -> {
                appSpecificationValue.setCreateTime(new Date());
            });
            pmsSpecificationValueMapper.insertOrUpdate(pmsSpecificationValues);
        }else {
            log.info("规格为空，商品为单规格商品");
        }


        //不能直接删除原来的skuStock，因为购物车里面存储了skuId
        //先保存更新原来的，再删除多余的
        List<AdminSkuSaveReqVO.PmsSkuStockVO> appSkuStockVOList = adminSkuSaveReqVO.getSkuStocks();
        if (CollectionUtil.isNotEmpty(appSkuStockVOList)) {
            List<PmsSkuStock> pmsSkuStockList = BeanUtils.toBeanList(appSkuStockVOList, PmsSkuStock.class);
            for (PmsSkuStock pmsSkuStock : pmsSkuStockList) {
                if (pmsSkuStock.getId() == null) {
                    pmsSkuStock.setCreateTime(new Date());
                }else {
                    pmsSkuStock.setUpdateTime(new Date());
                }
            }
            pmsSkuStockMapper.insertOrUpdate(pmsSkuStockList);
        }

        Set<Long> existSkuIds = appSkuStockVOList.stream()
                .map(AdminSkuSaveReqVO.PmsSkuStockVO::getId)
                .collect(Collectors.toSet());
        pmsSkuStockMapper.delete(
                new LambdaQueryWrapper<PmsSkuStock>()
                .eq(PmsSkuStock::getProductId,adminSkuSaveReqVO.getProductId())
                .notIn(PmsSkuStock::getId,existSkuIds)
        );
    }
}
